/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cinfo.siga.gestionacademica.ui.visual;

import java.util.LinkedList;
import java.util.List;
import org.cinfo.core.entidades.CursoModulo;
import org.cinfo.core.entidades.Modulo;
import org.cinfo.siga.gestionacademica.ui.modelos.ListaCursosDisponiblesListModel;
import org.cinfo.siga.gestionacademica.ui.modelos.TablaCursosModuloTableModel;
import org.openide.DialogDescriptor;
import org.openide.DialogDisplayer;

/**
 *
 * @author lartkma
 */
public class NuevoModuloDialog extends javax.swing.JPanel {

    TablaCursosModuloTableModel modeloTabla;
    ListaCursosDisponiblesListModel modeloLista;
    
    /**
     * Creates new form NuevoModuloDialog
     */
    public NuevoModuloDialog() {
        incializarDatos(new Modulo());
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The
     * content of this method is always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.JLabel jLabel1 = new javax.swing.JLabel();
        javax.swing.JLabel jLabel2 = new javax.swing.JLabel();
        javax.swing.JLabel jLabel3 = new javax.swing.JLabel();
        javax.swing.JLabel jLabel4 = new javax.swing.JLabel();
        codigoTextField = new javax.swing.JTextField();
        descripcionTextField = new javax.swing.JTextField();
        minimoAlumnosSpinner = new javax.swing.JSpinner();
        maximoCuotasSpinner = new javax.swing.JSpinner();
        javax.swing.JLabel jLabel5 = new javax.swing.JLabel();
        nombreTextField = new javax.swing.JTextField();
        tieneRepitenciaCheckbox = new javax.swing.JCheckBox();
        estaActivoCheckbox = new javax.swing.JCheckBox();
        jSeparator1 = new javax.swing.JSeparator();
        javax.swing.JLabel jLabel6 = new javax.swing.JLabel();
        javax.swing.JScrollPane jScrollPane1 = new javax.swing.JScrollPane();
        cursosDisponiblesList = new javax.swing.JList();
        asignarButton = new javax.swing.JButton();
        desasignarButton = new javax.swing.JButton();
        javax.swing.JScrollPane jScrollPane2 = new javax.swing.JScrollPane();
        cursosAsignadosTable = new javax.swing.JTable();
        nuevoCursoButton = new javax.swing.JButton();

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        org.openide.awt.Mnemonics.setLocalizedText(jLabel1, java.text.MessageFormat.format(java.util.ResourceBundle.getBundle("org/cinfo/siga/gestionacademica/ui/visual/Bundle").getString("NuevoModuloDialog.jLabel1.text"), new Object[] {})); // NOI18N

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        org.openide.awt.Mnemonics.setLocalizedText(jLabel2, java.text.MessageFormat.format(java.util.ResourceBundle.getBundle("org/cinfo/siga/gestionacademica/ui/visual/Bundle").getString("NuevoModuloDialog.jLabel2.text"), new Object[] {})); // NOI18N

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        org.openide.awt.Mnemonics.setLocalizedText(jLabel3, java.text.MessageFormat.format(java.util.ResourceBundle.getBundle("org/cinfo/siga/gestionacademica/ui/visual/Bundle").getString("NuevoModuloDialog.jLabel3.text"), new Object[] {})); // NOI18N

        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        org.openide.awt.Mnemonics.setLocalizedText(jLabel4, java.text.MessageFormat.format(java.util.ResourceBundle.getBundle("org/cinfo/siga/gestionacademica/ui/visual/Bundle").getString("NuevoModuloDialog.jLabel4.text"), new Object[] {})); // NOI18N

        minimoAlumnosSpinner.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(0), Integer.valueOf(0), null, Integer.valueOf(1)));

        org.openide.awt.Mnemonics.setLocalizedText(jLabel5, java.text.MessageFormat.format(java.util.ResourceBundle.getBundle("org/cinfo/siga/gestionacademica/ui/visual/Bundle").getString("NuevoModuloDialog.jLabel5.text"), new Object[] {})); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(tieneRepitenciaCheckbox, java.text.MessageFormat.format(java.util.ResourceBundle.getBundle("org/cinfo/siga/gestionacademica/ui/visual/Bundle").getString("NuevoModuloDialog.tieneRepitenciaCheckbox.text"), new Object[] {})); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(estaActivoCheckbox, java.text.MessageFormat.format(java.util.ResourceBundle.getBundle("org/cinfo/siga/gestionacademica/ui/visual/Bundle").getString("NuevoModuloDialog.estaActivoCheckbox.text"), new Object[] {})); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel6, java.text.MessageFormat.format(java.util.ResourceBundle.getBundle("org/cinfo/siga/gestionacademica/ui/visual/Bundle").getString("NuevoModuloDialog.jLabel6.text"), new Object[] {})); // NOI18N

        cursosDisponiblesList.setModel(modeloLista);
        jScrollPane1.setViewportView(cursosDisponiblesList);

        asignarButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/cinfo/siga/gestionacademica/ui/visual/iconos/arrow.png"))); // NOI18N
        asignarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                asignarButtonActionPerformed(evt);
            }
        });

        desasignarButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/cinfo/siga/gestionacademica/ui/visual/iconos/arrow-180.png"))); // NOI18N
        desasignarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                desasignarButtonActionPerformed(evt);
            }
        });

        cursosAsignadosTable.setModel(modeloTabla);
        jScrollPane2.setViewportView(cursosAsignadosTable);

        nuevoCursoButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/cinfo/siga/gestionacademica/ui/visual/iconos/document--plus.png"))); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(nuevoCursoButton, java.text.MessageFormat.format(java.util.ResourceBundle.getBundle("org/cinfo/siga/gestionacademica/ui/visual/Bundle").getString("NuevoModuloDialog.nuevoCursoButton.text"), new Object[] {})); // NOI18N
        nuevoCursoButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nuevoCursoButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(maximoCuotasSpinner))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(minimoAlumnosSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(59, 59, 59)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(tieneRepitenciaCheckbox)
                            .addComponent(estaActivoCheckbox))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jSeparator1)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(codigoTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(53, 53, 53)
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(nombreTextField))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel6)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 549, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                                    .addComponent(nuevoCursoButton)
                                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(asignarButton, javax.swing.GroupLayout.DEFAULT_SIZE, 58, Short.MAX_VALUE)
                                    .addComponent(desasignarButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(descripcionTextField)))
                        .addContainerGap())))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jLabel1, jLabel2, jLabel3, jLabel4});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(codigoTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(nombreTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(descripcionTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(minimoAlumnosSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tieneRepitenciaCheckbox))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(maximoCuotasSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(estaActivoCheckbox))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(56, 56, 56)
                                .addComponent(asignarButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(desasignarButton))
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 221, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(nuevoCursoButton)
                        .addGap(0, 29, Short.MAX_VALUE))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void nuevoCursoButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nuevoCursoButtonActionPerformed
        //TODO: try/catch
        NuevoCursoDialog nuevoCurso = new NuevoCursoDialog();
        DialogDescriptor dialogo = new DialogDescriptor(nuevoCurso, "Nuevo curso");
        DialogDisplayer.getDefault().notify(dialogo);
        if(dialogo.getValue() == DialogDescriptor.OK_OPTION){
            //TODO agregarCurso
            modeloLista.agregarCurso(nuevoCurso.getCurso());
        }
    }//GEN-LAST:event_nuevoCursoButtonActionPerformed

    private void asignarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_asignarButtonActionPerformed
        int indice = cursosDisponiblesList.getSelectedIndex();
        if(indice != -1){
            modeloTabla.agregarCursoModulo(new CursoModulo(modeloLista.getCurso(indice)));
            modeloLista.eliminarCurso(indice);
        }
    }//GEN-LAST:event_asignarButtonActionPerformed

    private void desasignarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_desasignarButtonActionPerformed
        int indice = cursosAsignadosTable.getSelectedRow();
        if(indice != -1){
            modeloLista.agregarCurso(modeloTabla.getCursoModulo(indice).getCurso());
            modeloTabla.eliminarCursoModulo(indice);
        }
    }//GEN-LAST:event_desasignarButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton asignarButton;
    private javax.swing.JTextField codigoTextField;
    private javax.swing.JTable cursosAsignadosTable;
    private javax.swing.JList cursosDisponiblesList;
    private javax.swing.JButton desasignarButton;
    private javax.swing.JTextField descripcionTextField;
    private javax.swing.JCheckBox estaActivoCheckbox;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSpinner maximoCuotasSpinner;
    private javax.swing.JSpinner minimoAlumnosSpinner;
    private javax.swing.JTextField nombreTextField;
    private javax.swing.JButton nuevoCursoButton;
    private javax.swing.JCheckBox tieneRepitenciaCheckbox;
    // End of variables declaration//GEN-END:variables
    
    public Modulo getModulo(){
        Modulo modulo = new Modulo();
        modulo.setCodigo(Integer.parseInt(codigoTextField.getText()));
        modulo.setDescripcion(descripcionTextField.getText());
        modulo.setMinimoAlumnos((Integer)minimoAlumnosSpinner.getValue());
        modulo.setMaximoCuotas((Integer)maximoCuotasSpinner.getValue());
        modulo.setFactorRepitencia(tieneRepitenciaCheckbox.isSelected());
        modulo.setActivo(estaActivoCheckbox.isSelected());
        modulo.setCursos(modeloTabla.getCursosModulo());
        return modulo;
    }

    private void incializarDatos(Modulo modulo) {
        if(modulo.getCodigo() != 0){
            codigoTextField.setText(modulo.getCodigo()+"");
            descripcionTextField.setText(modulo.getDescripcion());
            minimoAlumnosSpinner.setValue(modulo.getMinimoAlumnos());
            maximoCuotasSpinner.setValue(modulo.getMaximoCuotas());
            tieneRepitenciaCheckbox.setSelected(modulo.isFactorRepitencia());
            estaActivoCheckbox.setSelected(modulo.isActivo());
            modeloTabla = new TablaCursosModuloTableModel(modulo.getCursos());
        }else{
            modeloTabla = new TablaCursosModuloTableModel();
        }
        modeloLista = new ListaCursosDisponiblesListModel(/* TOODO obtenerListaCursos() */);
        limpiarListaCursosDeCursosYaSeleccionados();
    }

    private void limpiarListaCursosDeCursosYaSeleccionados() {
        LinkedList<Integer> aBorrar = new LinkedList<Integer>();
        for(int i=0; i<modeloLista.getSize(); i++){
            if(modeloTabla.estaCurso(modeloLista.getCurso(i)))
                aBorrar.add(i);
        }
        for(int i : aBorrar) modeloLista.eliminarCurso(i);
    }
}
